#include "Character.h"


using namespace std;


int Character::m_staticID = 0; /**< static integer. Variable is initialized to 0. An unique ID is created for each Character created. */

/**
 * Constructor
 * All members are set to 0 and the unique ID in incremented.
 */
Character::Character()
{
	m_strName = "";
	m_iHP = 0;
	m_iMaxHP = 0;
	m_iMP = 0;
	m_iMaxMP = 0;
	m_iXP = 0;
	m_iMaxXP = 0;
	m_iLevel = 1;
	m_iGold = 0;
	m_iPhysicalAttack = 0;
	m_iPhysicalDefense = 0;
	m_iMagicalAttack = 0;
	m_iMagicalDefense = 0;
	m_iEvasion = 0;
	m_rightHand = 0;
	m_leftHand = 0;
	m_body = 0;
	m_iStealth = 0;
	m_iPerception = 0;
	m_id = getNextID();
}

/**
 * Destructor
 * All items in the equipment list of the Character are destroyed.
 */
Character::~Character()
{
	for (unsigned int i = 0; i < m_equipment.size(); ++i)
	{
		Item* itemToDelete = m_equipment[i];
		delete itemToDelete;
		m_equipment.erase(m_equipment.begin() + i);
	}
}


/**
 * operator=
 * Overloaded assigment operator.
 * @param const Character& characterToCopy - which is the Character to copy
 * @return Character& - a reference to the copied Character
 *
 */
Character& Character::operator=(const Character& characterToCopy)
{
	if (this != &characterToCopy)
	{
		m_strName = characterToCopy.m_strName;
		m_iHP = characterToCopy.m_iHP;
		m_iMaxHP = characterToCopy.m_iMaxHP;
		m_iMP = characterToCopy.m_iMP;
		m_iMaxMP = characterToCopy.m_iMaxMP;
		m_iXP = characterToCopy.m_iXP;
		m_iMaxXP = characterToCopy.m_iMaxXP;
		m_iLevel = characterToCopy.m_iLevel;
		m_iGold = characterToCopy.m_iGold;
		m_iPhysicalAttack = characterToCopy.m_iPhysicalAttack;
		m_iPhysicalDefense = characterToCopy.m_iPhysicalDefense;
		m_iMagicalAttack = characterToCopy.m_iMagicalAttack;
		m_iMagicalDefense = characterToCopy.m_iMagicalDefense;
		m_iEvasion = characterToCopy.m_iEvasion;
		m_iStealth = characterToCopy.m_iStealth;
		m_iPerception = characterToCopy.m_iPerception;
		for (unsigned int i = 0; i < m_equipment.size(); ++i)
		{
			m_equipment[i] = characterToCopy.m_equipment[i];
			if (characterToCopy.m_equipment[i]->getId() == characterToCopy.m_rightHand->getId())
			{
				m_rightHand = m_equipment[i];
			}
			if (characterToCopy.m_equipment[i]->getId() == characterToCopy.m_leftHand->getId())
			{
				m_leftHand = m_equipment[i];
			}
			if (characterToCopy.m_equipment[i]->getId() == characterToCopy.m_body->getId())
			{
				m_body = m_equipment[i];
			}
			for (unsigned int i = 0; i < characterToCopy.m_backpack.size(); ++i)
			{
				if (characterToCopy.m_equipment[i]->getId() == characterToCopy.m_backpack[i]->getId())
				{
					m_backpack.push_back(m_equipment[i]);
					break;
				}
			}
		}
	}
	return *this;
}

/**
 * Get the next identifier for a character.
 * @param void
 * @return incremented ID
 */
int Character::getNextID()
{
	++(Character::m_staticID);
	return (Character::m_staticID);
}


